const express = require('express');

const routes = express.Router();

//引入mysql
const mysql = require('mysql');
//创建连接池
var pool = mysql.createPool({
    connectionLimit : 10,
    host : 'localhost',
    user : 'root',
    password : 'root',
    database : 'personmessage'
})

routes.get('/',(req,resp)=>{
    //获取cookies
    var uid = req.cookies.uid;
    var port_id = req.cookies.port_id;
    var time = req.cookies.getTimes;
    var loginame = req.cookies.loginame;

    //console.log(loginame)
    var hous = new Date(eval(time)).getHours();
    //console.log(hous)
    var minutes = new Date(eval(time)).getMinutes();
    var seconds = new Date(eval(time)).getSeconds();
    var houss = (hous < 10) ? '0'+hous : hous;
    var minus = (minutes < 10) ? '0'+minutes : minutes;
    var secondss = (seconds < 10) ? '0'+seconds : seconds;
    var date = houss+':'+minus+':'+secondss;
    //console.log(loginame)
    //每一页把条数据
    var param = 8;
    //将当前的页码传递出去作比较
    //如果首次登录教师账号  params 为空的情况下让它自动生成一个值 然后直接与下方list进行数学运算 默认查询第一页的数据
    var pos = (req.query.params) ? req.query.params : 1;
    //console.log(pos)
    //获取查询的页条数的起始位置
    var list = (pos * param) - param;
    //    执行数据查看   `` : 解析字符串里面的变量
    var sql = `SELECT permsg.* FROM permsg LIMIT ${list} , ${param}`;

    //console.log(sql)
    pool.getConnection(function(err,connection){
        if(err) throw err;
        //    链接成功
        connection.query(sql,(error,result)=> {
            var sql_msg = `SELECT COUNT(*) as sum FROM permsg`
            //    链接成功
            connection.query(sql_msg,(e,res)=> {
            //    释放连接   关闭数据库
            connection.release();

            //判断职务
            if(port_id == 1){
                var port = '普通员工';

            }else if(port_id == 2){
                var port = '行政主管';

            }else if(port_id == 3){
                var port = '财务总监';

            }

            //判断学历
            if(result[0].edu_id == 1){
                var education = '高中';

            }else if(result[0].edu_id == 2){
                var education = '大专';

            }else if(result[0].edu_id == 3){
                var education = '本科';

            }else if(result[0].edu_id == 4){
                var education = '硕士';

            }else if(result[0].edu_id == 5){
                var education = '博士';
            }
                var sum = res[0].sum;
                //判断需要多少页  条数不能为小数点  需要向上取整
                var params = Math.ceil( sum / param );
            //获取当前时间戳  Date.now()
            var data = new Date().getTime();
            resp.render('sel_permsg',{
                uid : uid,
                education : education,  //学历
                port_id : port_id, //职务
                result : result,
                port : port,
                date : date,  //当前时间
                loginame : loginame, //登录用户名
                params : params,
                pos : pos,  //当前页数
            })
            })
            //console.log(result)
            //sql语句出错执行报错处理
            if(error) throw error;
        })
    })

})
//暴露接口
module.exports = routes;
